<template>
  <!--修改角色-->
  <section class="component modify-role">
    <el-form :model="modifyRoleModel" :rules="modifyRoleRules" ref="modify-role" label-width="130px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model.trim="modifyRoleModel.roleName" size="small"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model.trim="modifyRoleModel.remark" :autosize="{ minRows:2, maxRows:4}" :maxlength="500"></el-input>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-row type="flex" justify="center">
          <el-button @click="close()">取消</el-button>
          <el-button @click="modifyRoleConfirmClick()">确定</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </section>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import { Dependencies } from '~/core/decorator'
import { Prop } from 'vue-property-decorator'
import { RoleService } from '~/service/service-manage/role.service'

@Component({})
export default class ModifyRole extends Vue {
  @Dependencies(RoleService)
  private roleService: RoleService

  @Prop({
    type: Object,
    required: true
  }) rowRoleObj: any

  // 添加角色数据模型
  private modifyRoleModel: any = {
    roleName: '',
    remark: '',
    id: ''
  }
  // 添加角色数据校验
  private modifyRoleRules: any = {
    roleName: [{required: true, message: '请输入角色名称', trigger: 'blur'}]
  }

  private refresh(rowRoleObj) {
    this.modifyRoleModel.roleName = rowRoleObj.roleName
    this.modifyRoleModel.remark = rowRoleObj.remark
    this.modifyRoleModel.id = rowRoleObj.id
  }

  /**
   * 确定
   */
  private modifyRoleConfirmClick() {
    let addForm: any = this.$refs['modify-role']
    addForm.validate((valid)=> {
      if (valid) {
        this.roleService.updateRole(this.modifyRoleModel).subscribe(
          data => {
            // 刷新数据
            this.$emit('refresh')
            // 关闭弹窗
            this.close()
            // 成功提示
            this.$message.success('修改成功')
          },
          ({msg}) => this.$message.error(msg)
        )
      }
    })
  }

  /**
   * 关闭弹窗
   */
  private close() {
    let addForm: any = this.$refs['modify-role']
    addForm.resetFields()
    this.$emit('close')
  }
}
</script>
